<?php
class Product extends MY_Controller
{
	public function __construct(){
		parent::__construct();
	}
	
	public function index($page=0){
		$data['title'] = '产品管理';
		$per_page = 10;
		
		$n = $this->db->get('product');
		
//		print_r($admins);
		$data['list_current'] = array('product','product');
		$data['js'] = array('product');
		
		
		$this->load->library ( 'pagination' );		
		$config ['base_url'] = site_url('/sysadmin/product/index');
		$config ['total_rows'] = $n->num_rows();
		$config ['per_page'] = $per_page;
		$config ['uri_segment'] = 4;
		$this->pagination->initialize ( $config );
		
		$o = $this->db->query("SELECT a.*, at.title as typename FROM {$this->db->dbprefix}product a 
		LEFT JOIN {$this->db->dbprefix}producttype at
		ON a.typeid = at.id
		ORDER BY a.id desc 
		LIMIT $page, $per_page");
		$values = $o->result();
		$data['values'] = $values;
		
		$data['pageinfo'] =  $this->pagination->create_links ();		
		$this->load->view('admin/header', $data);
		$this->load->view('admin/product', $data);
		$this->load->view('admin/footer');
	}
	function add($id=NULL){
		if($this->input->post('title')){
			$setarr = array(
				'title'=>$this->input->post('title'),
				'typeid'=>intval($this->input->post('typeid')),
				'image'=>$this->input->post('image'),
				'imagethumb'=>$this->input->post('imagethumb'),
				'content'=>$this->input->post('content'),
				'updatetime'=>date("Y-m-d H:i:s"));
			if(!$this->input->post('id')){//添加
				$setarr['createtime'] = $setarr['updatetime'];
				$this->db->insert('product', $setarr);
				echo $this->db->insert_id(); 
				die();
			}else{
				$this->db->where('id', $this->input->post('id'));
				if($this->db->update('product', $setarr)){
					echo 1;
				}else{
					echo -1;
				}
				
			}
		}else{
			if($id){//编辑时
				$a = $this->db->query("select a.*, at.title as pidSel
				from {$this->db->dbprefix}product a
				LEFT JOIN {$this->db->dbprefix}producttype at
				ON a.typeid = at.id
				where a.id=$id");
				echo json_encode($a->result());
			}else{
				echo '-1';
			}
			
		}
	}
	function delete($id=NULL){
		if($id){
			$id = explode('-', $id);
			$this->db->where_in('id', $id);
			if($this->db->delete('product')){
				echo 1;
			}else{
				echo -1;
			}
			
		}
	}
	//异步上传产品图片
	function upload_img(){
		$dir = "./data/upload/";
		$showdir = "/data/upload/";
		$yearmonth = date("Ym");
		if(!file_exists($dir.$yearmonth)){
			mkdir($dir.$yearmonth);
		}
		$time = time();
		$newpath = $dir.$yearmonth.'/'.$time;
		if ($_FILES ['proimg']) {
			$ext = end(explode('.', $_FILES ['proimg']['name']));  
			if(! in_array($ext, array('jpg','gif','png'))){
				echo json_encode( array('errormsg'=>'检查下图片类型 jpg|gif|png'));die();
			}
			if (move_uploaded_file ( $_FILES ['proimg'] ['tmp_name'],  $newpath.'.'.$ext )) {
				//处理缩略图
				$config['image_library'] = 'gd2';
				$config['source_image'] = $newpath.'.'.$ext;
				$config['create_thumb'] = TRUE;
				$config['maintain_ratio'] = TRUE;
				$config['width'] = 225;
				$config['height'] = 150;
				
				$this->load->library('image_lib', $config); 
				
				$this->image_lib->resize();
				echo json_encode ( array ('imgurl' => $showdir.$yearmonth.'/'.$time.'.'.$ext , 
				'thumburl' => $showdir.$yearmonth.'/'.$time.'_thumb.'.$ext) );
			} else {
				echo json_encode ( array ('errormsg' => '上传未成功' ) );
			}
		}else{
			echo json_encode ( array ('errormsg' => '没有文件' ) );
		}
		
	}
}